package com.hunter.common.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.GetMethod;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.apache.commons.httpclient.methods.StringRequestEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpClientUtil {

	private static final Logger logger = LoggerFactory.getLogger(HttpClientUtil.class);
	
	public static void httpGet(String url,Map<String, String> paramsMap) throws Exception{
		logger.info("http get:"+url);
		HttpClient hc = null;
		GetMethod md = null;
		try{
			hc = new HttpClient();
			md = new GetMethod(url);
			if (paramsMap!=null && paramsMap.size()>0) {
				List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
				for (String key : paramsMap.keySet()) {
					paramsList.add(new NameValuePair(key, paramsMap.get(key)));
				}
				md.setQueryString(paramsList.toArray(new NameValuePair[paramsList.size()]));
			}
			hc.executeMethod(md);
			logger.info("QueryString:"+md.getQueryString());
			logger.info("code:"+md.getStatusCode()+";response:"+md.getResponseBodyAsString());
		}finally{
			md.releaseConnection();
		}
	}
	
	public static String httpJsonPost(String url, String json) throws Exception{
		HttpClient httpClient = new HttpClient();
		PostMethod post = new PostMethod(url);
		//设置消息头
		post.addRequestHeader("Content-Type", "application/json;charset=utf-8");
//		post.addRequestHeader("Host", host);
		try{
			//添加json-body
			RequestEntity requestEntity = new StringRequestEntity(json,"text/json","UTF-8");
			post.setRequestEntity(requestEntity);
			httpClient.executeMethod(post);
			logger.info("httpjsonpost-response:"+post.getResponseBodyAsString());
			return post.getResponseBodyAsString();
		}finally{
			post.releaseConnection();
		}
	}
	
	public static String httpPost(String url, Map<String, String> paramsMap) throws Exception {
		HttpClient httpClient = new HttpClient();
		httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(10*1000);  
		PostMethod post = new PostMethod(url);
		// 设置消息头
		post.addRequestHeader("Content-Type", "text/html; charset=UTF-8");
		// post.addRequestHeader("Host", host);
		try {
			if (paramsMap != null && paramsMap.size() > 0) {
				List<NameValuePair> paramsList = new ArrayList<NameValuePair>();
				for (Entry<String, String> entry : paramsMap.entrySet()) {
					paramsList.add(new NameValuePair(entry.getKey(), entry
							.getValue()));
				}
				post.setQueryString(paramsList
						.toArray(new NameValuePair[paramsList.size()]));
				// post.setRequestBody(paramsList.toArray(new
				// NameValuePair[paramsList.size()]));
			}
			httpClient.executeMethod(post);
			logger.info("response:" + post.getResponseBodyAsString());
			return post.getResponseBodyAsString();
		} finally {
			post.releaseConnection();
		}
	}

}